<template>
  <view class="container" v-if="mainShow">
    <image
      :src="mainImg"
      mode="widthFix"
      :style="{
          height: scrollHeight + 'px',
          width: scrollWidth + 'px',
        }"
    ></image>
    <!-- <swiper class="swiper" style="width: 100%;" circular :indicator-dots="true" :autoplay="true" :interval="3000"
	:duration="500">
		<swiper-item>
			<image style="width: 100%;" mode="scaleToFill" src="@/static/images/m1.png"></image>
		</swiper-item>
		<swiper-item>
			<image style="width: 100%;"  mode="scaleToFill"  src="@/static/images/m2.png"></image>
		</swiper-item>
	</swiper> -->
    <!-- uni-card is-full :is-shadow="false">
	      <text class="uni-h6">英雄榜</text>
	</uni-card -->
    <!-- <uni-section title="英雄榜" type="line" style="background-color: transparent;" > -->
    <!-- <uni-section  type="line" style="background-color: transparent;" > -->
    <!-- <uni-table id="table" ref="table"  border stripe  emptyText="暂无更多数据" class="tabledata"  > -->
    <!-- <uni-tr>
			  		<uni-th width="80" align="center">姓名</uni-th>
			  		<uni-th width="30" align="center">王者</uni-th>
			  		<uni-th width="30" align="center">钻石</uni-th>
			  		<uni-th width="30" align="center">铂金</uni-th>
			  		<uni-th width="30" align="center">黄金</uni-th>
			  		<uni-th width="30" align="center">白银</uni-th>
			  	</uni-tr> -->
    <!-- <uni-tr v-for="(item, index) in rewardTop" :key="index">
			  		<uni-td >{{ item.staffName }}</uni-td>
			  		<uni-td align="center">{{ item.a1 }}</uni-td>
			  		<uni-td align="center">{{ item.a2 }}</uni-td>
			  		<uni-td align="center">{{ item.a3 }}</uni-td>
			  		<uni-td align="center">{{ item.a4 }}</uni-td>
			  		<uni-td align="center">{{ item.a5 }}</uni-td>
			  	</uni-tr> -->
    <!-- </uni-table>
		</uni-section> -->
    <!-- <table class="table">
			<tr v-for="(item,index) in rewardTop" :key="index">
				<td class="rank">{{index+1}}</td>
				<td class="name">{{item.staffName}}</td>
				<td class="king">{{item.a1}}</td>
				<td class="diamond">{{item.a2}}</td>
				<td class="platinum">{{item.a3}}</td>
				<td class="gold">{{item.a4}}</td>
				<td class="sliver">{{item.a5}}</td>
				<td class="total">{{item.cnt}}</td>
			</tr>
		</table> -->
  </view>
</template>

<script setup lang="ts">
import useMenuStore from "@/stores/modules/menu";
import { getUserLoginUrl, getUserToken } from "@/api/login";

import { listRewardTop } from "@/api/index";

const store = useMenuStore();
import { onLoad, onReady, onShow } from "@dcloudio/uni-app";
import { ref } from "vue";
// import { useRoute } from 'vue-router';

// const route = useRoute();
// let appId = route.params.appId; // 假设URL格式为 /users/:userId

const mainShow = ref(false);
const mainImg = ref("");

const goLogin = async () => {
  const res = await getUserLoginUrl("");
  if (res.code == 0) {
    let url = res.data;
    location.href = url;
  } else {
    uni.showToast({
      title: res.msg,
      icon: "error",
    });
  }
};
const goUserToken = async (data: any) => {
  const res = await getUserToken(data);
  if (res.code != 0) {
    if (res.code == -99) {
      //新进入的用户，有两个选择：自建组织，申请加入组织
      userData.level = 0; //data.level;
      uni.redirectTo({
        url:
          "/pages/create/first?appId=" +
          userData.appId +
          "&paras=" +
          userData.paras +
          "&sign=" +
          userData.sign +
          "&level=" +
          userData.level,
      });
    } else {
      uni.showToast({
        title: res.msg,
        icon: "error",
        style: "color:white",
      });
    }
  } else {
    //登录成功，数据写到store
    let data = res.data;
    //写登录数据
    uni.setStorageSync("token", res.data.token);
    uni.setStorageSync("orgInfo", JSON.parse(res.data.orgInfo));
    let token = res.data.token;
    mainShow.value = token ? true : false;
    // 显示tabbar
    uni.showTabBar({
      animation: false, // 是否需要动画效果，默认为 false
      success: function () {
        console.log("Tabbar is shown!");
      },
      fail: function (err) {
        console.error("Failed to show tabbar:", err);
      },
    });
    //拉取首页数据
    const { setMenu } = useMenuStore();
    let ui = JSON.parse(res.data.userInfo)
    setMenu(
      res.data.username,
      ui.permission,
      res.data.token,
      ui.buttons
    ); //将可访问的路由权限和用户名传给store
  }
};
const userData = {};
onLoad((option) => {
  let userInfo = uni.getStorageSync("loginInfo")
  let token = userInfo.token;
  mainShow.value = token ? true : false;
  let appId = option.appId;
  if (!appId) {
    //直接打开的首页
    if (!userInfo)
      //无登录信息
      goLogin();
    else {

      const store = useMenuStore();

      // 显示tabbar
      uni.showTabBar({
        animation: false, // 是否需要动画效果，默认为 false
        success: function () {
          console.log("Tabbar is shown!");
        },
        fail: function (err) {
          console.error("Failed to show tabbar:", err);
        },
      });
      getRewardTop(1)
    }
  } else {
    //189登录跳转回来了
    userData.appId = option.appId;
    userData.paras = option.paras;
    userData.sign = option.sign;
    goUserToken(option);
  }
  console.log("首页onLoad");
});
const getRewardTop= async (data: any) => {
			listRewardTop().then(response => {
				// this.rewardTop = response.rows;
        mainImg.value = "data:image/jpeg;base64,"+response.data
			});
		}
const scrollHeight = ref("800");
const scrollWidth = ref("400")
onShow(() => {
  console.log("首页onShow");
  getRewardTop(1)
  
});

onReady(() => {
  //这里不要用onLoad，要不然提示错误
  uni.getSystemInfo({
    success: (res) => {
      let query = uni.createSelectorQuery().select(".nvue-page-root");
      //想要获取高度的元素名（class/id）注意：这里要写scroll-view最外层的（class/id）
      query
        .boundingClientRect((data) => {
          //可用高度减去top高度要在里面设置，放在外面app端不生效
          scrollHeight.value = `${res.windowHeight }`;
          scrollWidth.value  = `${res.windowWidth }`;
        })
        .exec();
    },
  });
});
const input = ref(null);
</script>

<style lang="scss">
.title {
  padding: 20px;
  font-size: 20px;
  font-weight: bold;
}
.title2 {
  padding: 10px;
  font-size: 18px;
}
</style>
